/**
 * 
 */
package com.zhiteng.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.zhiteng.entity.Item;
import com.zhiteng.mapper.ItemMapper;
import com.zhiteng.service.ItemService;
import com.zhiteng.utils.common.DateUtil;
import com.zhiteng.utils.common.IDUtils;
import com.zhiteng.vo.QueryItemVo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**  
* <p>Title: ItemServiceImpl.java</p>  
* <p>Description: 积分礼品Service</p>  
* @author dragon  
* @date 2018年12月26日  
* @version 1.0  
*/
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;

	/*@Override
	//	新增或更新积分礼品
	public int saveItem(Item item,HttpSession session) throws Exception {
		MultipartFile file = item.getImageFile();
		if(item.getId()==null) {	//	新增
			item.setId(IDUtils.genItemId());
			item.setCreated(new Date());
			String imgURL = uploadImg(file,session);
			item.setImage(imgURL);
			return itemMapper.insert(item);
		}else {	//	更新
			if(file.getOriginalFilename().startsWith("/upload/itemImgs")) {	//	图片未更改
				item.setImage(null); 	//	图片地址不用更新
			}else {	//	图片更改了
				String imgURL = uploadImg(file,session);
				item.setImage(imgURL);
			}
			return itemMapper.updateByPrimaryKeySelective(item);
		}
	}
	
	private String uploadImg(MultipartFile file,HttpSession session) throws Exception {
		//	获取真实路径
		String realPath = session.getServletContext().getRealPath("/upload/itemImgs");
		//	获取拓展名（.jpg,包括小圆点） 
		String extName = file.getOriginalFilename()
				.substring(file.getOriginalFilename().lastIndexOf("."));
		//	利用UUID生成服务器端保存的文件名
		String tmpName = IDUtils.getUUID()+extName;
		String fileName = realPath + "/" + tmpName;
		//	上传图片
		file.transferTo(new File(fileName));
		//	设置数据库中存储的图片的url(相对路径)
		return "/upload/itemImgs/"+tmpName;
	}*/
	
	//	新增或更新积分礼品
	@Override
	public int saveItem(Item item) {
		if(item.getId()==null) {	//	新增
			item.setId(IDUtils.genItemId());
			item.setCreated(new Date());
			return itemMapper.insert(item);
		}else {	//	更新
			return itemMapper.updateByPrimaryKeySelective(item);
		}
	}
	
	
	@Override
	public List<Item> queryItemsByVo(QueryItemVo queryItemVo) {
		Example example = new Example(Item.class);
		//	设置select字段
		example.selectProperties("id","title","price","num","rule","created","image","description");
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(queryItemVo.getCreatedStr()))
			try {	//	指定日期之后上架的
				criteria.andGreaterThan("created", DateUtil.String2Date(queryItemVo.getCreatedStr().trim(),"yyyy-MM-dd"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		if(StringUtils.isNotBlank(queryItemVo.getCategory()+""))
				criteria.andEqualTo("category", queryItemVo.getCategory());
		if(StringUtils.isNotBlank(queryItemVo.getTitle())) 
				criteria.andLike("title", "%"+queryItemVo.getTitle().trim()+"%");
		return itemMapper.selectByExample(example);
	}

	@Override
	public int deleteItemById(long itemId) {
		return itemMapper.deleteByPrimaryKey(itemId);
	}

	@Override
	public Item queryItemById(long itemId) {
		return itemMapper.selectByPrimaryKey(itemId);
	}

	@Override
	public int updateItem(Item item) {
		return itemMapper.updateByPrimaryKeySelective(item);
	}

	@Override
	public List<Item> queryNewItems() {
		//	紧跟着PageHelper.startPage(2, 12)的第一个select方法会被分页
		//		-- 获取第1页6条内容
		PageHelper.startPage(1, 6);	//	查询最新上架的六件礼品
		Example example = new Example(Item.class);
		example.selectProperties("id","title","price","num","rule","created","image");
		example.orderBy("created").desc(); //	创建日期降序排列
		List<Item> itemList = itemMapper.selectAll();
		return itemList;
	}


}
